<?php
namespace Admin\Model;
use Think\Model;
class OrderModel extends Model{

  public function getTotalNums_before($map=''){

        $data = $this->where($map)->sum('order_num');

        return $data;
    }




   	//获取用户的累计下单数量 加上月额
    public function getTotalNums($map=''){

        $data = $this->where($map)->sum('order_num');
       
        return $data;
    }
       //递归获取上级用户的总订单数  判断工资订单
     public function getOrderNums_up(&$userid,&$orderxf_num,&$audit_time){

        $userinfo = M('user')->find($userid);
        $up_id=(int)$userinfo['f_id'];
        $invite_id=$userinfo['invite_id'];

        if ($up_id>0) {

          $data['team_id']=$up_id;
          $data['totalnum']=$orderxf_num;
          $data['addtime']=$audit_time;
          M('order_team_fh')->add($data);
          M('user')->where('id='.$up_id)->setInc('fh_ordernums',$orderxf_num);
            $up_userinfo=M('user')->where("id=$up_id")->find();
              if ($up_userinfo['status']>1) {
            $map_xf['user_id']=$userid;
            $map_xf['paystatus']=1;
                     switch ($up_userinfo['status']) {
                    case '2':
                         $each=10;
                         $fh_map['addtime']=array('gt',$up_userinfo['sj_time']);
                         $map_xf['audit_time']=array('GT',$up_userinfo['sj_time']);
                        break;
                    case '3':
                        $each=20;
                        $fh_map['addtime']=array('gt',$up_userinfo['sj3_time']);
                        $map_xf['audit_time']=array('GT',$up_userinfo['sj3_time']);
                        break;
                    case '4':
                        $each=40;
                        $fh_map['addtime']=array('gt',$up_userinfo['sj4_time']);
                        $map_xf['audit_time']=array('GT',$up_userinfo['sj4_time']);
                        break;    
                    default:
                        # code...
                        break;
                }
                $orderxf_fhnum=$this->get_xf_total_nums($up_id);
                $fh_map['id']=$up_id;
               // $team_ordernum=M('order_team_fh')->where($fh_map)->sum('totalnum');
                $team_ordernum=$up_userinfo['fh_ordernums'];
               
                
             
             if ($team_ordernum>=$each) {
              //平分工资订单
              if ($up_userinfo['f_id']==0) {

                  $this->half_fh_gz($up_id,$team_ordernum,$each);

              }else{
                $this->normal_fh_gz($up_id,$team_ordernum,$each);
              }
                
                
       }
            }


              

       $this->getOrderNums_up($up_id,$orderxf_num,$audit_time);

   }

  return true;

    }

   //从下到上获取团队的累计下单数量 累计三个月 sj_time 递归传递升级时间
    public function getTeamTotalNumsUpCum(&$userid,&$startTime='',&$endTime='',&$sj_time=''){
      $userinfo = M('user')->find($userid);
        $up_id=$userinfo['invite_id'];
        $up_userinfo = M('user')->find($up_id);
        $up_status=$up_userinfo['status'];
        $jb=M('jbset')->find();
        D('user')->set_xj_userstatus($userid);//执行下更新状态注册
         $self_totalnum=$this->get_group_num_bytime($userid,$startTime,$endTime);

                switch ($userinfo['status']) {

                    case '1':
                       $monthly=$jb['sj_jb2_orders'];
                       $total_con=$monthly*$jb['sj_jb2_mons'];
                       $save_sj['sj_ordernum']=1;
                       //$save_sj_time['sj_time']=time();
                       $sj_time_str='sj_time';
                       $sj_invite='sj_invite';
                        break;
                    case '2':
                         $monthly=$jb['sj_jb3_orders'];
                         $total_con=$monthly*$jb['sj_jb3_mons'];
                          $save_sj['sj3_ordernum']=1;
                           //$save_sj_time['sj3_time']=time();
                           $sj_time_str='sj3_time';
                           $sj_invite='sj3_invite';
                        break;
                    case '3':
                          $monthly=$jb['sj_jb4_orders'];;
                          $total_con=$monthly*$jb['sj_jb4_mons'];
                           $save_sj['sj4_ordernum']=1;
                            //$save_sj_time['sj4_time']=time();
                            $sj_time_str='sj4_time';
                            $sj_invite='sj4_invite';
                        break;
                    
                    default:
                        # code...

            }

             if ($self_totalnum>=$total_con ) {
              
                M('user')->where("id=$userid")->save($save_sj);  
               $sj_invite= M('user')->where("id=$userid")->getField($sj_invite);
               // D('user')->set_xj_userstatus($userid);//执行下更新状态
               if ($sj_invite) {
                  $up_status=$userinfo['status']>=4?$userinfo['status']:$userinfo['status']+1;
                  $up_time=$this->get_last_time($userid,$startTime,$endTime);
                  
                  switch ($userinfo['status']) {
                    case '2':
                      $each=10;
                      break;
                    case '3':
                      $each=20;
                      break;
                    case '4':
                      $each=40;
                      break;
                    
                    default:
                      # code...
                      break;
                  }
                  if ($up_status>2) {
                    $reset_user[$sj_time_str]=$up_time;
                    $reset_user['status']=$up_status;
                    $reset_user['id']=$userid;
                    $reset_user['self_ztxf']=$userinfo['fh_ordernums']%10;
                    $reset_user['self_flordernum']=$userinfo['self_flordernum']%10;
                    $reset_user['fh_ordernums']=$userinfo['fh_ordernums']%$each;
                  }else{
                     $reset_user[$sj_time_str]=$up_time;
                    $reset_user['status']=$up_status;
                    $reset_user['id']=$userid;
                    $reset_user['self_ztxf']=0;
                    $reset_user['self_flordernum']=0;
                    $reset_user['fh_ordernums']=0;
                  }
                  
                  M('user')->save($reset_user); 
                  $lowlist=M('user')->where('invite_id='.$userid)->select();
                  foreach ($lowlist as $key => $value) {
                    if ($value['status']<$up_status) {
                                M('user')->where('id='.$value['id'])->setField('f_id',$value['invite_id']);
                                  
                            } 
                  }


                  M('auth_group_access_home')->where("uid=$userid")->setField("group_id",$up_status);
                          $up_up_id=$userinfo['invite_id'];
                          $up_up_status=M('user')->where('id='.$up_up_id)->getField('status');
                    

               if ($up_up_id>0) {
                      if ($up_status==$up_up_status) {
                        
                             M('user')->where('id='.$userid)->setField('f_id',0);
                          }

                      if ($up_status>$up_up_status) {
                             $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$up_up_id;
                          $beyond['f_id']=-1;
                      M('user')->where("id=$userid")->save($beyond);
                          $c=M('user')->where("invite_id=$userid")->select();
                      
                        foreach ($c as $key => $value) {
                           if ($value['status']<$up_status) {
                                M('user')->where('id='.$value['id'])->setField('f_id',$value['invite_id']);
                                  
                            } 
                        }

                          }
               }



             
            }
       
    }
    if ($up_id>0) { 
            $this->getTeamTotalNumsUpCum($up_id,$startTime,$endTime,$up_time);
        }
}

    

    //获取团队的累计下单数量截止目前
    public function getTeamTotalNumsNow($userid){
      
       $data=M('order_team')->where('team_id='.$userid)->sum('totalnum');
       return $data;
    }


      //获取团队的累计下单数量根据时间
    public function getTeamTotalNums($userid,$startTime='',$endTime=''){
       $map['team_id']=$userid;
      $map['addtime'] = array(array('egt',$startTime),array('elt',$endTime),'AND');
      $data=M('order_team')->where($map)->count();
        return $data;
    }



    //获取多个用户的总订单数
     public function getOrderNums_users($ids,$map=''){
            $con['user_id'] = array ('in',$ids);
           $con['paystatus'] = 1;
           $data = $this->where($con)->count();
        
        return $data;
    }

     public function getOrderNums_fl(&$userid,&$orderxf_num){
         $userinfo = M('user')->find($userid);
           $invite_id=$userinfo['invite_id']; 
           if ($invite_id>0) {
           $data['team_id']=$invite_id;
          $data['totalnum']=$orderxf_num;
          $data['addtime']=time();
          M('order_team')->add($data);
          M('user')->where('id='.$invite_id)->setInc('team_ordernums',$orderxf_num);
           $this->getOrderNums_fl($invite_id,$orderxf_num);
        }

    }



    //正常返还工资订单

    public function normal_fh_gz($up_id,$team_ordernum,$each)
    {         $curtime=time();
            $yfh_gz=floor($team_ordernum/$each);
            $userinfo=M('user')->find($up_id);
            $gz['user_id']=$up_id;
            $gz['desc']='团队业绩达标';
            $gz['pj']='0';
            $gz['isself_xj']=1;
            $gz['user_status']=$userinfo['status'];
            $fh_gz=M('order_gz')->where($gz)->count();
            $n=$yfh_gz-$fh_gz;
            for ($i=1; $i <=$n; $i++) { 
                unset($data);
                  $moshi=M('moshi')->where('status=1')->find();
                  $jifen=M('jifen')->find();
                $data['user_id']=$up_id;
                $data['addtime']=$curtime;
                $data['desc']='团队业绩达标';
                $data['fh_weeks']=$moshi['moshi_gz_weeks'];
                $data['fh_cfjf']=$jifen['jifen_cf_gz'];
                $data['isself_xj']=1;
                $data['user_status']=$userinfo['status'];
                    $a = date("w",$data['addtime']);
            if($a =="0"){
                  $d=date("Y-m-d 00:00:00",$data['addtime']);
            }else{
                $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['addtime']));
            }
                 $data['begin_hf_time'] =strtotime($d)+604800; 
           
                $addgz=M('order_gz')->add($data);
                  $gz=M('order_gz')->find($addgz);
              if ($gz) {
                for ($i=1; $i <= $gz['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$userid;
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$addgz)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }
              }
                

            }
    }


      //自身消费10单，返还一半工资订单的逻辑
     public function self10_half_getOrderNums_up($userid,$self_ordernum,$desc='',$is_half=1){
            $user_info=M('user')->find($userid);
            $curtime=time();
         $yfh_gz=floor($self_ordernum/10);
            $map['user_id']=$userid;
            $gz['user_id']=$userid;
            $gz['desc']=$desc;
             $gz['isself_xj']=1;
             $gz['user_status']=$user_info['status'];
            if ($is_half>1) {
                 $gz['pj']='平级业绩达标';
                }else{
                   $gz['pj']=0;
                }
            $fh_gz=M('order_gz')->where($gz)->count();
           
            $n=$yfh_gz-(int)$fh_gz;
              $moshi=M('moshi')->where('status=1')->find();
            for ($i=1; $i <=$n ; $i++) { 

                  $curtime=time();
            //D('order')->where($map)->setField('self_gz_status',1);
               unset($data);
               $jifen=M('jifen')->find();
                $data['user_id']=$userid;
                $data['addtime']=$curtime;
                $data['fh_cfjf']=$jifen['jifen_cf_gz']/$is_half;
                $data['desc']=$desc;
                $data['isself_xj']=1;
                $data['fh_weeks']=$moshi['moshi_gz_weeks'];
                $data['user_status']=$user_info['status'];
                if ($is_half>1) {
                 $data['pj']='平级业绩达标';
                }
                    $a = date("w",$data['addtime']);
            if($a =="0"){
                  $d=date("Y-m-d 00:00:00",$data['addtime']);
            }else{
                $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['addtime']));
            }
     $data['begin_hf_time'] =strtotime($d)+604800; 

                $addgz=M('order_gz')->add($data);
                if ($is_half>1) {
                $data2=$data;
                $data2['user_id']=$user_info['invite_id'];
                //M('user')->find($user_info['invite_id']);
                $data2['isself_xj']=0;
                 $addgz2=M('order_gz')->add($data2);
                }


                  $gz=M('order_gz')->find($addgz);
             
                 for ($i=1; $i <= $gz['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$userid;
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$addgz)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }

              if ($addgz2) {
               $gz2=M('order_gz')->find($addgz2);

                for ($i=1; $i <= $gz2['fh_weeks']; $i++) { 

                $data_fh2['gz_id'] =$addgz2;
               
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=".$gz2['user_id'])->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$gz2['user_id'];
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz2['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$addgz2)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }

             }
            
               return;
            }
           
        
    }



       public function half_fh_gz($up_id,$team_ordernum,$each)
    {        $curtime=time();
            $yfh_gz=floor($team_ordernum/$each);
            $userinfo=M('user')->find($up_id);
            $gz['user_id']=$up_id;
            $gz['desc']='团队业绩达标';
            $gz['pj']='平级业绩达标';
            $gz['isself_xj']=1;
            $gz['user_status']=$userinfo['status'];
      
            $fh_gz=M('order_gz')->where($gz)->count();
            $n=$yfh_gz-$fh_gz;
      
            for ($i=1; $i <=$n; $i++) { 
                unset($data);
                  $moshi=M('moshi')->where('status=1')->find();
                  $jifen=M('jifen')->find();
                $data['user_id']=$up_id;
                $data['addtime']=$curtime;
                $data['desc']='团队业绩达标';
                $data['fh_weeks']=$moshi['moshi_gz_weeks'];
                $data['fh_cfjf']=$jifen['jifen_cf_gz']/2;
                 $data['pj']='平级业绩达标';
                 $data['isself_xj']=1; 
                 $data['user_status']=$userinfo['status'];
                    $a = date("w",$data['addtime']);
            if($a =="0"){
                  $d=date("Y-m-d 00:00:00",$data['addtime']);
            }else{
                $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['addtime']));
            }
                 $data['begin_hf_time'] =strtotime($d)+604800; 
           
                $addgz=M('order_gz')->add($data);
                  $gz=M('order_gz')->find($addgz);
                  $data2=$data;
               $data2['user_id']=$userinfo['invite_id'];   
               $data2['isself_xj']=0;   
                $addgz2=M('order_gz')->add($data2);
                  $gz2=M('order_gz')->find($addgz);

              if ($gz) {
                 for ($i=1; $i <= $gz['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$userid;
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$addgz)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }
              }

             if ($gz2) {
              unset($data_fh2);
                 for ($i=1; $i <= $gz2['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz2;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz2['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$gz2['user_id'];
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz2['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$addgz2)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }
              } 
                

            }
    }





      //递归获取上级用户的总订单数  判断累计订单是否升级
     public function getOrderNums_up_sj(&$userid){

        $userinfo = M('user')->find($userid);
        $up_id=$userinfo['invite_id'];
        $up_status=M('user')->where("id=$up_id")->getField('status');
        if ($up_id) {
          $con_team['team_id']=$up_id;
          //$team_ordernum=M('order_team')->where($con_team)->sum('totalnum');
          $team_ordernum=$userinfo['ordernums']+$userinfo['team_ordernums'];
                //如果上级在业务员或者以上，则计算自己所在团队的业绩
                switch ($up_status) {
                    case '1':
                         $each=60*3;
                        break;
                    case '2':
                        $each=360*3;
                        break;
                    case '3':
                        $each=2160*3;
                        break;    
                    default:
                        # code...
                        break;
                }
              
                if ($team_ordernum>=$each) {
                 
                       $up_status=$up_status>=4?$up_status:$up_status+1; 


                    }
               

            
              $this->getOrderNums_up_sj($up_id);

        }
        return false;
        
    }

//自身直接推荐的'.$j['jb1'].'满10单'
    public function xf_total_nums($userid='',$is_half=1)
    { 
      $curtime=time();
      if (empty($userid)) {
        return;
      }

      $map['f_id']=$userid;
      $map['status']=1;
      $info=M('user')->where($map)->field('id')->select();
      $f_id=M('user')->where('id='.$userid)->field('f_id')->find();
      $user_info=M('user')->where('id='.$userid)->find();
      switch ($user_info['status']) {
        case '2':
         $sj_time=$user_info['sj_time'];
          break;
        case '3':
          $sj_time=$user_info['sj3_time'];
          break;
        case '4':
          $sj_time=$user_info['sj4_time'];
          break;
        
        default:
          # code...
          break;
      }

      if (!empty($sj_time)) {
        $map2['audit_time']=array('GT',$sj_time);
      }
      foreach ($info as $key => $value) {
        $ids[]=$value['id'];
      }
      if (count($ids)>1) {
          $map2['user_id']  = array('in',$ids);
      }else{
        $map2['user_id']=$ids[0];
      }
    
      $map2['paystatus']  = 1;

    $r= $user_info['self_ztxf'];
   // $r=  M('order')->where($map2)->sum('order_num');

  $j=M('jbset')->find();
   

             $yfh_gz=floor($r/10);
            $gz['user_id']=$up_id;
            $gz['desc']='自身直接推荐的'.$j['jb1'].'满10单';
            $gz['isself_xj']=1;
            $gz['user_status']=$user_info['status'];
            if ($is_half>1) {
              $gz['pj']='平级业绩达标';
            }else{
              $gz['pj']='0';
            }
            $fh_gz=M('order_gz')->where($gz)->count();
            $n=$yfh_gz-$fh_gz;
            

            for ($i=1; $i <=$n; $i++) { 
                unset($data);
                  $moshi=M('moshi')->where('status=1')->find();
                  $jifen=M('jifen')->find();
                $data['user_id']=$userid;
                $data['addtime']=$curtime;
                $data['desc']='自身直接推荐的'.$j['jb1'].'满10单';
                $data['fh_weeks']=$moshi['moshi_gz_weeks'];
                $data['fh_cfjf']=$jifen['jifen_cf_gz']/$is_half;
                $data['isself_xj']=1;
                $data['user_status']=$user_info['status'];
                if ($is_half>1) {
                  $data['pj']='平级业绩达标';
                }

                    $a = date("w",$data['addtime']);
if($a =="0"){
      $d=date("Y-m-d 00:00:00",$data['addtime']);
}else{
    $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['addtime']));
}
     $data['begin_hf_time'] =strtotime($d)+604800; 
                $addgz=M('order_gz')->add($data);
                if ($is_half>1) {
                $data2=$data;
                $data2['user_id']=$user_info['invite_id'];
                $data2['isself_xj']=0;
                 $addgz2=M('order_gz')->add($data2);
                }


                  $gz=M('order_gz')->find($addgz);
             if ($gz) {
                for ($i=1; $i <= $gz['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$userid;
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$addgz)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }
             }

             if ($addgz2) {
               $gz2=M('order_gz')->find($addgz2);

                for ($i=1; $i <= $gz2['fh_weeks']; $i++) { 

                $data_fh2['gz_id'] =$addgz2;
               
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=".$gz2['user_id'])->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$gz2['user_id'];
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$addgz2)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }

             }


             return;   

            }

            return;
    

    }


   

       public function get_xf_total_nums($userid='')
    {

      if (empty($userid)) {
        return;
      }
     
      $map2['u_id']=$userid;
    
    $userinfo=M('user')->find($userid);
      switch ($userinfo['status']) {
                   
                    case '2':
                         $map2['addtime']  = array('gt',$userinfo['sj_time']);
                        break;
                    case '3':
                         $map2['addtime']  = array('gt',$userinfo['sj3_time']);
                        break;
                     case '4':
                          $map2['addtime']  = array('gt',$userinfo['sj4_time']);
                        break;
                    
                    default:
                        # code...

            }
    $r=  M('order_ztxf')->where($map2)->sum('ordernum');
      return $r;
    }


     //升级条件获取订单数量 自身加团队
    public function get_group_num($id,$months)
    {
      $end=time();

         $begin=mktime(0,0,0,date('m')-$months,1,date('Y'));
         
       $team_map['team_id']=$id;
         $team_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');

              $order_map['user_id']=$id;
         $order_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');
         //pjb/sj_convar_dump($search);exit();
       $cur_count= M('user')->where($search)->count();
       $num1 = M('order_team')->where($team_map)->sum('totalnum');
       $num2 = M('order')->where($order_map)->sum('order_num');
       $cur_num=$num1+$num2;
       return $cur_num;
    }




     //升级条件获取订单数量 自身加团队 根据时间端
    public function get_group_num_bytime($id,$begin='',$end='')
    {
  
         
       $team_map['team_id']=$id;
         $team_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');
              $order_map['user_id']=$id;
              $order_map['paystatus']=1;
         $order_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');
         //pjb/sj_convar_dump($search);exit();
       $cur_count= M('user')->where($search)->count();
       $num1 = M('order_team')->where($team_map)->sum('totalnum');
       $num2 = M('order')->where($order_map)->sum('order_num');
       $cur_num=$num1+$num2;
       return $cur_num;
    }
    //根据用户获取最新下单的时间
     public function get_last_time($id,$begin='',$end='')
    {

       $team_map['team_id']=$id;
         $team_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');
              $order_map['user_id']=$id;
              $order_map['paystatus']=1;
         $order_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');
     
       $time1 = M('order')->where($team_map)->order('id desc')->getField('audit_time');
       $time2 = M('order')->where($order_map)->order('id desc')->getField('audit_time');
       $cur_time=$time2>$time1?$time2:$time1;
       return $cur_time;
    }


     public function getTeamTotalNumsUpCum_info(&$userid,&$startTime='',&$endTime=''){
      $userinfo = M('user')->find($userid);
        $up_id=$userinfo['invite_id'];
        $up_userinfo = M('user')->find($up_id);
        $up_status=$up_userinfo['status'];
        $jb=M('jbset')->find();

         $self_totalnum=$this->get_group_num_bytime($userid,$startTime,$endTime);
                switch ($userinfo['status']) {

                    case '1':
                       $monthly=$jb['sj_jb2_orders'];
                       $total_con=$monthly*$jb['sj_jb2_mons'];
                       $save_sj['sj_ordernum']=1;
                       $save_sj_time['sj_time']=time();
                       $sj_invite='sj_invite';
                        break;
                    case '2':
                         $monthly=$jb['sj_jb3_orders'];
                         $total_con=$monthly*$jb['sj_jb3_mons'];
                          $save_sj['sj3_ordernum']=1;
                           $save_sj_time['sj3_time']=time();
                           $sj_invite='sj3_invite';
                        break;
                    case '3':
                          $monthly=$jb['sj_jb4_orders'];;
                          $total_con=$monthly*$jb['sj_jb4_mons'];
                           $save_sj['sj4_ordernum']=1;
                            $save_sj_time['sj4_time']=time();
                            $sj_invite='sj4_invite';
                        break;
                    
                    default:
                        # code...
                 

            }
       

             if ($self_totalnum>=$total_con ) {
              
                M('user')->where("id=$userid")->save($save_sj);  
               $sj_invite= M('user')->where("id=$userid")->getField($sj_invite);
               if ($sj_invite) {

                  $up_status=$userinfo['status']>=4?$userinfo['status']:$userinfo['status']+1;
               
                  M('user')->where("id=$userid")->setField("status",$up_status); 
                  M('user')->where("id=$userid")->save($save_sj_time); 
                  M('auth_group_access_home')->where("uid=$userid")->setField("group_id",$up_status);
                  
                   if ($userinfo['invite_id']>0) {
                          $up_up_id=$userinfo['invite_id'];
                          $up_up_status=M('user')->where('id='.$up_up_id)->getField('status');
                        if ($userinfo['status']>=$up_up_status  ) {

                         $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$up_up_id;
                      M('user')->where("id=$userid")->save($beyond);

                  }
                    }

               }

             
            }

}


}
